package com.xiansiblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiansiblog.dao.BlogDao;
import com.xiansiblog.entity.Comment;
import com.xiansiblog.query.DetailedBlog;
import com.xiansiblog.query.FirstPageBlog;
import com.xiansiblog.query.RecommendBlog;
import com.xiansiblog.service.BlogService;
import com.xiansiblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.rmi.runtime.Log;

import java.util.List;

/**
*@Description: 首页控制器
*@Author: 弦思
*@Date 2021/2/3
*/
@Controller
public class IndexController {
    @Autowired
    private BlogDao blogDaol;
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;

    //分页查询博客列表
    @GetMapping("/")
    public String index(Model model,
                        @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,6);
        //第一页博客展示
        List<FirstPageBlog> allFirstPageBlog = blogService.getAllFirstPageBlog();
        //最热博客推荐
        List<RecommendBlog> recommendedBlog = blogService.getRecommendedBlog();
        PageInfo<FirstPageBlog> firstPageBlogPageInfo = new PageInfo<>(allFirstPageBlog);
        model.addAttribute("pageInfo",firstPageBlogPageInfo);
        model.addAttribute("recommendedBlogs",recommendedBlog);
        return "index";
    }
    //搜索博客 SQL语句难点
    @PostMapping("/search")
    public String search(Model model,
                         @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                         @RequestParam String query){
        PageHelper.startPage(pageNum,10);
        List<FirstPageBlog> searchBlog = blogService.getSearchBlog(query);
        PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(searchBlog);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("query", query);
        return "search";
    }

    //跳转博客详情页面
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model){
        DetailedBlog detailedBlog = blogService.getDetailedBlog(id);
        List<Comment> comments = commentService.listCommentByBlogId(id);
        model.addAttribute("comments", comments);
        model.addAttribute("blog", detailedBlog);
        return "blog";
    }

    //博客信息
    @GetMapping("/footer/blogmessage")
    public String blogMessage(Model model){
        int blogTotal = blogService.getBlogTotal();
        int blogViewTotal = blogService.getBlogViewTotal();
        int blogCommentTotal = blogService.getBlogCommentTotal();
        int blogMessageTotal = blogService.getBlogMessageTotal();
        model.addAttribute("blogTotal", blogTotal);
        model.addAttribute("blogViewTotal",blogViewTotal);
        model.addAttribute("blogCommentTotal",blogCommentTotal);
        model.addAttribute("blogMessageTotal",blogMessageTotal);
        return "index :: blogMessage";
    }
}
